﻿@{
    var singlelineDataTypes = new string[] { "MultilineText", "ImageUrl" };
}
@if (Model == null)
{
    @: @ViewData.ModelMetadata.NullDisplayText
}
else if (ViewData.TemplateInfo.TemplateDepth > 1)
{
    @: @ViewData.ModelMetadata.SimpleDisplayText
}
else
{
    int cols = 2;
    int curr = 0;
    int colspan = 1;
    var props = ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm) && !pm.IsComplexType);
    int totalCols = props.Count();
    <table id="display-table" class="display-table" cellpadding="0" cellspacing="0" border="0">
        @foreach (var prop in props)
        {
            colspan = 1;
            if (singlelineDataTypes.Contains(prop.DataTypeName)) 
            {
                colspan = 3;
                if (curr % cols == cols - 1 || curr == totalCols - 1)
                {
                    curr++;
                @:</tr>
                }
            }
            if (curr % cols == 0)
            {
                if (singlelineDataTypes.Contains(prop.DataTypeName))
                {
                    curr++;
                }
                @:<tr>
            }
                <td class="caption">
                    <div class="display-label" style="text-align: right;">
                        @prop.GetDisplayName()
                    </div>
                </td>
                <td colspan="@colspan">
                    <div class="display-field">
                        @Html.Display(prop.PropertyName, new { data = this.Model })
                    </div>
                </td>
            if (curr % cols == cols - 1 || curr == totalCols - 1)
            {
            @:</tr>
            }
        curr++;
        }
    </table>
}
<script type="text/javascript">
    $(function () {
        $('#display-table tr').each(function () {
            if ($('td', this).length == 4)
                return;
            $('td', this).eq(1).attr('colspan', '3');
        });
    });
</script>